
var dataBase = {
	seatMap : [],
};

//var selectSeats = []; //当前选中的座位

/*
思考一下逻辑，大部分应该是a，小部分是b或者d，应该采取替换的原则，而不能用status来直接设置状态(虽然这个方法比较简便)
*/

// var initSeatMap = [
// 	'aaaaaaaaaaaa',
// 	'aaaaaaaaaaaa',
// 	'bbbbbddddddd',
// 	'bbbbbddddddd',
// 	'bbbbbbbbbbbb'
// ];
// dataBase["seatMap"] = initSeatMap;

dataBase["seatMap"] = initSeatMap;

// 获取数据
var initData = {
	seatList : function(receiveData) {
		// 渲染座位图
		renderData.seatMap();
	}
}

// 渲染数据
var renderData = {
	seatMap : function(data) {
		// var rows = $('#seat-rows').val();
		// var cols = $('#seat-cols').val();
		// var x = cols;
		// var y = rows;
		// var map = [];
		// for(var i=0;i<x;i++){
		// 	map[i]="";
		// 	for(var j=0;j<y;j++){
		// 		map[i]+="a";
		// 	}
		// }
		// dataBase["seatMap"] = map;

		var seatMap = dataBase["seatMap"];
		console.log('=====最终seatMap=====');
		console.log(seatMap);

		$('#seat-map').empty();
		$("#seat-map-legend").empty();

		var sc = $('#seat-map').seatCharts({
			map: seatMap,
			naming : {
				getLabel : function (character, row, column) {
					return '';
				}
			},
			seats: seatCategory,
			click: function () {
				if (this.status() == 'available') {
					// $('<li>'+(this.settings.row+1)+'排'+this.settings.label+'座</li>')
					// 	.attr('id', 'cart-item-'+this.settings.id)
					// 	.data('seatId', this.settings.id)
					// 	.appendTo($cart);
					//
					// $counter.text(sc.find('selected').length+1);
					// $total.text(recalculateTotal(sc)+price);

					//console.log('row:'+this.settings.row+1);
					//console.log('label:'+this.settings.label);
					//selectSeats[this.settings.row+1] = this.settings.label;

					console.log('row:'+this.settings.row+';label:'+this.settings.label+';id'+this.settings.id);

					//准备编辑单个座位
					var coordinate = this.settings.id;
					$('#J_coordinate').val(coordinate);
					$('#J_single_coordinate').html(); //先清空
					$('#J_single_class').html();
					$('#J_single_tag').html();
					$('#J_regionId').val('');
					$('#J_pai').val(''); //先清空
					$('#J_zuo').val(''); //先清空

					var param = {};
					param.hid = hid;
					param.coordinate = coordinate;
					$.get(url_singleSeat, param, function(ret){
						if(ret.code == 1){
							var data = ret.data;
							$('#J_single_coordinate').html('x:'+data.x+';y:'+data.y);
							$('#J_single_class').html(data.class_name);
							$('#J_single_tag').html(data.tag);
							$('#J_regionId').val(data.regionId);

							var hallSeatInfo = data.hallSeatInfo;
							if(hallSeatInfo){
								$('#J_pai').val(hallSeatInfo.pai);
								$('#J_zuo').val(hallSeatInfo.zuo);
							}
						}else if(ret.code == 0){
							Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
						}
					},'json');

					//selectSeats.push(this.settings.id);

					return 'selected';
				} else if (this.status() == 'selected') {
					//seat has been vacated
					return 'available';
				} else if (this.status() == 'unavailable') {
					//seat has been already booked
					return 'unavailable';
				} else {
					return this.style();
				}
			}
		});

		//=====这边改的话是直接更改了状态

		//已售出的座位
		//sc.get(['1_2', '4_4','4_5','6_6','6_7','8_5','8_6','8_7','8_8', '10_1', '10_2']).status('unavailable');

		//不可选座位
		//sc.get(['3_2']).status('seat-disabled');

		//空白区域
		//sc.get(['4_4']).status('seat-blank');

		//已经单独设置的座位
		//sc.get(singleCoordinate).status('available seat-single');

		//已经单独设置的座位，设置对应的座位
		for (var t in hallSeatList) {
			//console.log(hallSeatList[t]);
			//$("#" + singleCoordinate[t]).addClass('seat-single')
			$("#" + hallSeatList[t].coordinate).html(hallSeatList[t].zuo);
		}

		// for (var t in singleCoordinate) {
		// 	console.log(singleCoordinate[t]);
		// 	//$("#" + singleCoordinate[t]).addClass('seat-single')
		// 	$("#" + singleCoordinate[t]).html('1')
		// }

		// 设置座位容器宽度
		interaction.setSeatContainerWidth();

		//selectSeats = []; //清空

		//计算总金额
		function recalculateTotal(sc){
			var total = 0;
			sc.find('selected').each(function () {
				total += price;
			});

			return total;
		}
	}
}

// 交互
var interaction = {
	// 设置座位容器宽度
	setSeatContainerWidth : function() {
		var mapMaxSeatCount = dataBase["seatMap"][0].length;
		var seatWidth = $(".seatCharts-cell").outerWidth(true);
		seatWidth = seatWidth;
		mapMaxSeatCount = mapMaxSeatCount + 1;
		console.log('seatWidth:'+seatWidth);
		console.log('mapMaxSeatCount:'+mapMaxSeatCount);
		$("#seat-map").width(mapMaxSeatCount * seatWidth);
		//alert(window.innerWidth / (mapMaxSeatCount * seatWidth));
		// var scale = window.innerWidth / (mapMaxSeatCount * seatWidth);
		// //alert(scale);
		// $('#renderSeat').css({
		// 	transform : 'scale(' + scale + ')'
		// })
	},
};

//生成座位图
function generate_grid(){
	var rows = $('#seat-rows').val();
	var cols = $('#seat-cols').val();
	var x = cols;
	var y = rows;
	var map = new Array();
	for(var i=1;i<=x;i++){
		var tmp = '';
		for(var j=1;j<=y;j++){
			tmp += "x";
		}
		map.push(tmp);
	}
	//console.log(map);
	dataBase["seatMap"] = map;

	initData.seatList();
}
//添加座位类型
$('a.J_addType').on('click', function(e){
	e.preventDefault();

	var url = $(this).attr('href');
	var title = $(this).data('tip');
	var param;
	$.get(url, param, function(data){
		var dataset;
		try{
			dataset = $.parseJSON(data);
			if(dataset.status == 'error'){
				Wei.Util.wapTipNew({msg: dataset.msg, icon: 2});
			}
		}catch(e){
			layer.open({
				type: 1,
				closeBtn: 1, //不显示关闭按钮
				shift: 2,
				//shadeClose: true, //开启遮罩关闭
				area: ['auto'],
				title: title,
				content: data
			});
		}
	},'text');
});
//设置座位类型
function mark_seats(){
	//获取选中的座位
	//获取已选择的座位，原先用selectSeats数组，但是感觉不太准确
	var selectSeats = [];
	var seatCell = $(".seatCharts-cell");
	seatCell.each(function() {
		var mapId = $(this).attr("id");
		var checkFlag = $(this).hasClass("selected");
		if(checkFlag == true){
			selectSeats.push(mapId);
		}
	})

	var stype = $("input[name='seat-marker']:checked").val();
	//console.log('stype:'+stype);
	if(!stype){
		Wei.Util.wapTipNew({msg: '请选择座位类型，再设置座位', icon: 2, data: ''});
		return;
	}

	var x = initCols;
	var y = initRows;

	//尝试循环seatMap
	var oriMapArr = dataBase["seatMap"];
	console.log(oriMapArr);
	var newMapArr = new Array();
	for(var i=1;i<=x;i++){
		console.log(i)
		var colStr = oriMapArr[i-1];
		var colArr = colStr.split('');
		var tmpStr = '';
		for(var j=1;j<=y;j++){
			if(stype){
				if(selectSeats.indexOf(i+'_'+j) == -1){
					tmpStr += colArr[j-1];
				}else{
					tmpStr += stype;
				}
			}
		}
		//console.log(tmpStr);
		newMapArr.push(tmpStr);
	}
	//console.log(newMapArr);
	dataBase["seatMap"] = newMapArr;

	//更新数据库
	var param = {
		hid : hid,
		rows : initRows,
		cols : initCols,
		stype : stype
	};
	param.selectSeats = JSON.stringify(selectSeats);

	$.post(url_saveSeatsRegion, param, function(ret){
		if(ret.code == 1){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 1, data: ret.data}, function(){
				layer.closeAll();
				//Wei.Util.readyToJump(ret);
			});
			initData.seatList();
		}else if(ret.code == 0){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
		}
	},'json');

	// var map = new Array();
	// var seatCell = $(".seatCharts-cell");
	// seatCell.each(function() {
	// 	var mapId = $(this).attr("id");
	//
	// 	var checkFlag = $(this).hasClass("seat-available");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData1.push(mapId);
	// 	}
	//
	// 	var checkFlag = $(this).hasClass("seat-disabled");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData2.push(mapId);
	// 	}
	//
	// 	var checkFlag = $(this).hasClass("seat-blank");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData3.push(mapId);
	// 	}
	// })
	// dataBase["seatMap"] = map;

	//使用横排数列来循环
	// var rows = $('#seat-rows').val();
	// var cols = $('#seat-cols').val();
	// var x = cols;
	// var y = rows;
	// var map = new Array();
	// for(var i=1;i<=x;i++){
	// 	var tmp = "";
	// 	for(var j=1;j<=y;j++){
	// 		if(stype == 'seat-disabled'){
	// 			if(selectSeats.indexOf(i+'_'+j) == -1){
	// 				console.log('1');
	// 				tmp += "a";
	// 			}else{
	// 				console.log('2');
	// 				tmp += "d";
	// 			}
	// 		}else if(stype == 'seat-blank'){
	// 			if(selectSeats.indexOf(i+'_'+j) == -1){
	// 				console.log('3');
	// 				tmp += "a";
	// 			}else{
	// 				console.log('4');
	// 				tmp += "b";
	// 			}
	// 		}else{
	// 			console.log('5');
	// 			tmp += "a";
	// 		}
	// 	}
	// 	map.push(tmp);
	// }
	// dataBase["seatMap"] = map;

	// var map = [];
	// for(var i=0;i<x;i++){
	// 	map[i]="";
	// 	for(var j=0;j<y;j++){
	// 		if(stype == 'no-seats'){
	// 			var tmpI = i+1;
	// 			var tmpJ = j+1;
	// 			if(selectSeats.indexOf(tmpI+'_'+tmpJ) == -1){
	// 				console.log('1');
	// 				map[i]+="a";
	// 			}else{
	// 				console.log('2');
	// 				map[i]+="_";
	// 			}
	// 		}else{
	// 			console.log('3');
	// 			map[i]+="a";
	// 		}
	// 	}
	// }
}
//生成座位图
$('.saveSeatsMap').on('click', function(e){
	var param = {};
	param.hid = hid;
	param.rows = $('#seat-rows').val();
	param.cols = $('#seat-cols').val();

	var seatData1 = [];
	//var seatData2 = new Array();
	//var seatData3 = new Array();

	var seatCell = $(".seatCharts-cell");
	//把三种座位类型，post过去
	seatCell.each(function() {
		var mapId = $(this).attr("id");

		var checkFlag = $(this).hasClass("seatCharts-cell");
		//console.log('checkFlag:'+checkFlag)
		if(checkFlag == true){
			seatData1.push(mapId);
		}

		// var checkFlag = $(this).hasClass("seat-blank");
		// //console.log('checkFlag:'+checkFlag)
		// if(checkFlag == true){
		// 	seatData2.push(mapId);
		// }
		//
		// var checkFlag = $(this).hasClass("seat-disabled");
		// //console.log('checkFlag:'+checkFlag)
		// if(checkFlag == true){
		// 	seatData3.push(mapId);
		// }
	})
	//console.log(seatData);

	param.seatData1 = JSON.stringify(seatData1);
	//param.seatData2 = JSON.stringify(seatData2);
	//param.seatData3 = JSON.stringify(seatData3);

	$.post(url_saveSeatsMap, param, function(ret){
		if(ret.code == 1){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 1, data: ret.data}, function(){
				layer.closeAll();
				//Wei.Util.readyToJump(ret);
			});
		}else if(ret.code == 0){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
		}
	},'json');
});

//单独设置选中座位
$('button.J_singleSubmit').on('click', function(e){
	e.preventDefault();
	var btn = $(this),
		form = btn.parents('form.J_ajaxForm');

	var coordinate = $('#J_coordinate').val();
	var zuo = $('#J_zuo').val();

	form.ajaxSubmit({
		url: btn.data('action') ? btn.data('action') : form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
		dataType: 'json',
		contentType: "application/x-www-form-urlencoded;charset=utf-8",
		beforeSubmit: function (arr, $form, options){
		},
		success: function (ret, statusText, xhr, $form){
			var text = btn.text();
			//按钮文案、状态修改
			//btn.removeClass('disabled').text(text.replace('中...', '')).parent().find('span').remove();
			//btn.text(text.replace('中...', ''))
			//btn.removeClass('disabled').text(text.replace('中...', ''));
			if(ret.code == 1){
				$('#'+coordinate).html(zuo);
			}else if(ret.code == 0){
				Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
				btn.prop('disabled', false);
			}
		},
		error: function (ret, statusText, xhr, $form){
		}
	});
});

$('a.J_showRegion').on('click', function(e){
	var showtype = $(this).data('showtype');
	//已经单独设置的座位，设置对应的座位
	for (var t in hallSeatList) {
		//console.log(hallSeatList[t]);
		//$("#" + singleCoordinate[t]).addClass('seat-single')
		if(showtype == 'pai'){
			$("#" + hallSeatList[t].coordinate).html(hallSeatList[t].pai);
		}else if(showtype == 'zuo'){
			$("#" + hallSeatList[t].coordinate).html(hallSeatList[t].zuo);
		}else if(showtype == 'letter'){
			$("#" + hallSeatList[t].coordinate).html(hallSeatList[t].region_letter);
		}else if(showtype == 'tag'){
			$("#" + hallSeatList[t].coordinate).html(hallSeatList[t].region_tag);
		}
	}
});

$('a.J_calNum').on('click', function(e){
	var param = {
		hid: hid
	};
	$.get(url_calNum, param, function(ret){
		if(ret.code == 1){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 1, data: ret.data}, function(){
				layer.closeAll();
				//Wei.Util.readyToJump(ret);
			});
		}else if(ret.code == 0){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
		}
	},'json');
});

if(initRows){
	initData.seatList();
}